#include "lms_demo.h"

static char g_testLmsPool[2048];

static VOID user_overflow(VOID)
{
	printf("\n-----------------over flow check start--------------\n");
	// 申请内存
    char *str = (char *)LOS_MemAlloc(g_testLmsPool, 20);
	// 越界访问
    printf("str[20]=%d\n", str[20]);
	printf("\n-----------------over flow check end--------------\n");
}
static VOID use_after_free(VOID)
{
	printf("\n-----------------use after free start--------------\n");
	char *str = (char *)LOS_MemAlloc(g_testLmsPool, 20);
	// 释放
    LOS_MemFree(g_testLmsPool, str);
	// 访问已经释放的内存
    printf("str[%2d]=0x%2x ", 0, str[0]); 
	printf("\n-----------------use after free end--------------\n");
}

VOID lms_task(VOID)
{ 
	// 初始化内存池
    UINT32 ret = LOS_MemInit(g_testLmsPool, 2048);
    if (ret != 0) {
        printf("%s failed, ret = 0x%x\n", __FUNCTION__, ret);
        return;
    }
	user_overflow();
	use_after_free();
}
UINT32 lms_entry(VOID){
    UINT32 ret, tid;    
    // 锁住任务调度，防止高优先级任务调度
    LOS_TaskLock();
    TSK_INIT_PARAM_S param={0};    
    /* 创建用于lms测试的任务 */    
    memset(&lmsTestTask, 0, sizeof(TSK_INIT_PARAM_S));    
    param.pfnTaskEntry = (TSK_ENTRY_FUNC)lms_task;
    param.pcName       = "TestLmsTsk";    /* 测试任务名称 */    				     
    param.uwStackSize  = 0x2000;    
    param.usTaskPrio   = 5;    
    //lmsTestTask.uwResved   = LOS_TASK_STATUS_DETACHED;    
    ret = LOS_TaskCreate(&tid, &param);    
    if(ret != LOS_OK){        
        printf("LmsTestTask create failed .\n");            
    } 
    // 解锁任务调度
    LOS_TaskUnlock();
    return LOS_OK;
}
